package com.wormholestack.tomcat.test;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description： 嵌入式Tomcat启动  访问http://localhost:8080/tomcat/info
 * @Author MRyan
 * @Date 2022/12/20 23:27
 * @Version 1.0
 */
public class ServletDemo {

    public static void main(String[] args) throws LifecycleException {
        HttpServlet servlet = new HttpServlet() {
            @Override
            protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().write("嵌入式Tomcat启动");
            }
        };
        Tomcat tomcat = new Tomcat();
        Context context = tomcat.addContext("/tomcat", null);
        tomcat.addServlet("/tomcat", "info", servlet);
        context.addServletMappingDecoded("/info", "info");
        tomcat.init();
        tomcat.start();
        tomcat.getServer().await();

    }


}
